<?php session_start();
if(!isset($_SESSION['login']))
{
    $_SESSION['login'] = false;
    $_SESSION['username'] = "dummy";
}
?>

<html>
<head>
    
<script type="text/javascript">
function validator()
{
  
    var user = document.getElementById('usr').value;
    var pass = document.getElementById('pass').value;


if (user.length==0 || pass.length==0)
  {
  if(user.length == 0) {alert("Masukkan username anda terlebih dahulu");}    
  else if(pass.length == 0) {alert("Masukkan password anda terlebih dahulu");}
  
  document.getElementById("validCheck").innerHTML="";
  return;
  }
  
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  
  
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    
    if(xmlhttp.responseText == "berhasil")
    {
        //window.location="login_succeed.php";
        window.location="login.php";
    }
    else
    {alert("anda gagal masuk!");}    
    }   
  }
xmlhttp.open("GET","login_validate.php?u="+user+"&p="+pass,true);
xmlhttp.send();
}
</script>
</head>
<body>

    <?
        if($_SESSION['login'] == true)
        {
            echo "welcome, ".$_SESSION['username']."!<br>";
        }
        else
        {
            echo '<p><b>Masukkan Username dan Password anda :</b></p>';
            echo '<form>';
            echo 'Username: <input id="usr" type="text" size="20" /><br>';
            echo 'Password: <input id="pass" type="password" size="20"/><br>';
            echo '<button type="button" onclick="validator()">Validasi</button>';
            echo '</form>';
            echo '<p>Hasil: <span id="validCheck"></span></p>';
        }
    ?>
  
</body>
</html>
